package com.pw.study.structure.linkedList;

/**
 * @description:
 * @author: linux_future
 * @date: 2022/11/19
 */
public class MyLinkedList {
    private ListNode head;

    public static void main(String[] args) {
        MyLinkedList m = new MyLinkedList();
        m.addLast(4);
        m.addLast(2);
        m.addLast(1);
        m.addLast(3);
        m.display();
        System.out.println("--------------------");
        Solution s = new Solution();
        ListNode tmp = s.sortList(m.head);
        m.displayMyLinkedList(tmp);

    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //第一个节点
        if (this.head == null) {
            this.head = node;
        } else {
            //第二个节点
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //显示链表
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //给定头节点节点显示链表
    public void displayMyLinkedList(ListNode cur) {
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

}


/**
 * 节点类
 */
class ListNode {
    public final int val;
    public ListNode next;

    public ListNode(int data) {
        this.val = data;
    }

    @Override
    public String toString() {
        return "Node:" + val;
    }
}
